local Element = import("./Element.lua");

local CheckBox = commonlib.inherit(Element);

CheckBox:Property("Checked", false, "IsChecked");
CheckBox:Property("DefaultStyle", "width: 20px; height: 20px; border: 2px solid #b5b5b5; padding: 2px;");

function CheckBox:ctor()
    self:SetName("CheckBox");
end

function CheckBox:Init(xmlnode, window, parent)
    CheckBox._super.Init(self, xmlnode, window, parent);
    return self;
end

function CheckBox:OnAttrValueChange(attrName, attrValue)
    if (attrName == "checked") then
        self:SetChecked(self:GetAttrBoolValue("checked"));
    end
end

function CheckBox:RenderContent()
    if (self:IsChecked()) then 
        local x, y, w, h = self:GetContentPosition();
        self:SetPenColor(0xb5, 0xb5, 0xb5);
        self:SetLineWidth(2);
        self:DrawLine(x + w * 1 / 4, y + h * 1 / 2, x + w / 2, y + h * 3 / 4);
        self:DrawLine(x + w / 2, y + h * 3 / 4, x + w * 3 / 4, y + h / 4);
    end
end

function CheckBox:OnMouseDown(event)
    if (event:IsLeftButton()) then
        self:SetChecked(not self:IsChecked());
    end
end

return CheckBox;